using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.XtraReports.UI;
using System.IO;

public partial class Rpt_PrintView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && Request.Params["doc"] != null)
        {
            string reportName = Request.Params["doc"].ToString();
            string no = Request.Params["no"].ToString();
            

            //doc

            //bill
            //invoice 50
            //51:estimaate

            string userName =HttpContext.Current.User.Identity.Name;
            MemoryStream ms = new MemoryStream();
            string fileName = "";
            try
            {
                if (reportName == "50")
                {
                    ms = PrintInvoice(no);
                    fileName = "Invoice" + no;
                }
                else if (reportName == "51")
                {
                    ms = PrintEstimate(no);
                    fileName = "Estimate" + no;
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message+ex.StackTrace+ex.InnerException);
            }
            ExChange(ms, fileName);


        }
    }
 private void ExChange(MemoryStream ms, string fileName)
    {
        MemoryStream str = ms;

        byte[] bt = str.GetBuffer();

        try
        {
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Length", bt.Length.ToString());
            Response.AddHeader("Content-Disposition", "inline; filename=" + fileName + ".pdf");
            //Response.Output.Write(bt);
            Response.BinaryWrite(bt);
        }
        catch (Exception ex)
        {
            Response.Clear();
            Response.Write(ex.Message + "/" + ex.StackTrace);
        }
        ms.Dispose();
        str.Dispose();
    }

    private MemoryStream PrintInvoice(string invN)
    {
        XtraReport rpt = new XtraReport();
        //rpt.LoadLayout(Server.MapPath(@"~\Rpt\Report\CfsInvoice.repx"));
            rpt.LoadLayout(Server.MapPath(@"~\Reports\Invoice.repx"));
        // connect database
        rpt.DataSource = DocPrint.dsInvoice(invN);

        System.IO.MemoryStream str = new MemoryStream();
        rpt.ExportToPdf(str);
        return str;
    }
    private MemoryStream PrintEstimate(string invN)
    {
        XtraReport rpt = new XtraReport();
        //rpt.LoadLayout(Server.MapPath(@"~\Rpt\Report\CfsInvoice.repx"));
            rpt.LoadLayout(Server.MapPath(@"~\Reports\Quotation.repx"));
        // connect database
        rpt.DataSource = DocPrint.dsEstimate(invN);

        System.IO.MemoryStream str = new MemoryStream();
        rpt.ExportToPdf(str);
        return str;
    }

}
